package com.zygh.policeasset.utils;

import com.zygh.policeasset.pojo.DepRate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DepRateUtil {
    /**
     * 计算账面价值
     *
     * @param depRateList
     */
    public List<DepRate> getDepRate(List<DepRate> depRateList) throws ParseException {
        for (int i = 0; i < depRateList.size(); i++) {
            DepRate depRate = depRateList.get(i);

            //金额
            String m = depRate.getMoney();
            double money = Double.parseDouble(m);

            //折旧率
            int rate = Integer.valueOf(String.valueOf( depRate.getDepreciationYear()))*12;

            //现折旧（月）
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String purchaseTime = depRate.getCreateTime();
            String nowTime = sdf.format(new Date());
            Calendar bef = Calendar.getInstance();
            Calendar aft = Calendar.getInstance();
            bef.setTime(sdf.parse(purchaseTime));
            aft.setTime(sdf.parse(nowTime));
            int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
            int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
            int time = Math.abs(month + result);

            //账面净值
            double b = 0.00;
            if (time == 0){
                b = money-money/rate;
            }else {
                b = money-(money*time)/rate;
            }
            BigDecimal bookMoney = new BigDecimal(b).setScale(2, RoundingMode.UP);
            depRate.setBookMoney(bookMoney.doubleValue());
        }
        return depRateList;
    }
}
